home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.internetmci.com!iol!usenet
- From: David Byrden <goyra@iol.ie>
- Newsgroups: comp.lang.c++
- Subject: Re: HELP: Algorithm for ordering (x,y) coordinates
- Date: 16 Mar 1996 21:55:40 GMT
- Organization: Ireland On-Line
- Message-ID: <4ifdcs$m8o@nuacht.iol.ie>
- References: <314B0266.456A@mit.edu>
- NNTP-Posting-Host: dialup-184.dublin.iol.ie
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22KIT (Windows; I; 16bit)
-
-
- Imran Haq <ihaq@mit.edu> wrote:
- >Here is a problem that I can't seem to express in code easily using STL:
- >
- >I'm trying to order a set of (x,y) coordinates in an anitclockwise sense. To
- >compute whether the coordinates are indeed anticlockwise, I'm computing the
- >normal vector perpendicular to a plane defined by (overlapping) subsets containing
- >3 nodes each. If the normal vector is positive, the order is correct, otherwise
- >I just swap coordinate positions.
- >
- >The problem is that I'm trying to use STL's generic sort algorithm to do this
- >and supplying a custom comparison function. However, the form of this function
- >requires only two arguments, not the 3 I need to compute order. In fact, what
- >I really need is to have this custom function automatically accept the
- >first coordinate pair as a reference point, and modify the order of the 2nd and 3rd
- >points if they are not anticlockwise.
- >
- >Any suggestions? Or do I need to write a sort algorithm outside what STL provides?
-
-
- I think I can just about see what you are doing. I presume that there is
- one fixed reference point that you use for all the triplets.
-
- The STL will handle this fine. What you need is to use a functor, not a
- function, with the sort algorithm. A functor is an object with operator()
- defined; in this case, operator() would have the same signature as the
- function you have been using so far.
-
- The great thing is that, because a functor is an object, it can carry
- around the third point within itself. A functor is rather like a fuction
- with attached data.
-
- Here's a suggestion;
-
-
- struct clockwise{
- point pt ;
- clockwise( const point& p ) : pt(p) { }
- bool operator() ( const point& a, const point& b )
- {
- return /* some doodah involving a, b and pt */ ;
- }
- } ;
-
-
- sort( bunch.begin(), bunch.end(), clockwise( point( 0,0 ) ) ) ;
-
-
- David
-
-
-
-